<?php
require_once "curlhelper.php";
class Common {          
    
    public function echoIfExist($str) {
        if ($str != NULL)
            echo $str;
    }

    public function dump($var, $die = FALSE, $depth = 1000, $color = TRUE) {
        CVarDumper::dump($var, $depth, $color);
        if ($die)
            die();
    }

    public function redirect($route) {
        $redirectUrl = Yii::app()->createUrl($route);
        Yii::app()->controller->redirect($redirectUrl);
        echo "<script type='text/javascript'>top.location.href = '$redirectUrl';</script>";
    }

    public function secondToHour($second, $type) {
        $h = intval($second / 3600);
        if ($type == 'h')
            return $h;
        $m = intval(($second - $h * 3600) / 60);
        if ($type == 'm')
            return $m;
        $s = $second - $h * 3600 - $m * 60;
        if ($type == 's')
            return intval($s);
    }

    public function getTheme() {
        $defaultTheme = 'pc';
        $defaultSmartPhoneTheme = 'smartphone';
        $defaultWapTheme = 'wap';
        
        $session = new CHttpSession();
        $session->open();        
        if (isset($session['theme']))
            return $session['theme'];
        else {
            if (self::isRunningiOS() || self::isRunningAndroidOS())
                return $defaultSmartPhoneTheme;
            elseif(self::isSymbian()) {
                return $defaultWapTheme;
            }else{
                return $defaultTheme;
            } 
        }        
    }

    public function setTheme($theme) {
        $session = new CHttpSession();
        $session->open();
        $session->timeout = 60;
        $session['theme'] = $theme;
    }

    public function isMobile() {
        $detect = new MobileDetect();
        if($detect->isMobile()){
            return TRUE;
        }else{
            return FALSE;
        }        
    }

    public function requireTheme($theme) {
        if (Yii::app()->theme->name != $theme)
            Yii::app()->controller->redirect(Yii::app()->homeUrl);
    }

    public function alert($message, $redirectUrl = NULL) {
        $this->render('application.components.views.alert', array('message' => $message, 'redirectUrl' => $redirectUrl)); 
        Yii::app()->end();
    }

    public function getCurrentUrl() {
        return strtolower(Yii::app()->controller->id . '/' . Yii::app()->controller->action->id);
    }

    function isMultiArrayEmpty($multiarray) {
        if (is_array($multiarray) and !empty($multiarray)) {
            $tmp = array_shift($multiarray);
            if (!is_multiArrayEmpty($multiarray) or !is_multiArrayEmpty($tmp)) {
                return false;
            }
            return true;
        }
        if (empty($multiarray)) {
            return true;
        }
        return false;
    }     

    function getCurrentThemePath() {
        return Yii::app()->createUrl('themes/' . Yii::app()->theme->name);
    }
   
    function echoWithXssFilter($str) {
        $this->beginWidget('CHtmlPurifier');
        echo $str;
        $this->endWidget();
    }

    function check_email_address($email) {
        // First, we check that there's one @ symbol, and that the lengths are right 
        if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
            // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. 
            return false;
        } // Split it into sections to make life easier 
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
                return false;
            }
        }
        if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
            // Check if domain is IP. If not, it should be valid domain name 
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain                
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
                    return false;
                }
            }
        }
        return true;
    }

    function echoWithDot($number) {
        echo number_format($number, 0, ',', '.');
    }
    
    function setCookieLogin($url){
        Yii::app()->request->cookies['url_before_login'] = new CHttpCookie('url_before_login', $url);
    }
    
    function unsetCookieLogin(){
        unset(Yii::app()->request->cookies['url_before_login']);
    }
    
    function simpleSlug($str)
    {        
        $slug = "";     
        $slug = strip_tags(trim(html_entity_decode($str, ENT_QUOTES, 'UTF-8'), "\xc2\xa0"));
        $slug = Myvietstring::removeInvalidCharacter($slug);        
        $slug = preg_replace('!\s+!', ' ', $slug);
        $slug = trim($slug);
        $slug = Myvietstring::convertToUnsignedString($slug);          
        $slug = strtolower($slug);        
        return $slug;
    }        
		
    //Kiem tra la dang chay tren mobile hoac table
    //Se tra ve 'true' neu la iOS, 'false' neu la Android
    function isSupport()
    {
        $detect = new MobileDetect();
        if($this->isRunningMobile())
        {
            return $detect->is('iOS') || $detect->is('AndroidOS');
        }
        return false;
    }

    function isRunningiOS() 
    {
//        $detect = new MobileDetect();
//        return $detect->is('iOS');
        //if (preg_match("/iP(od|hone|ad)/i", $_SERVER["HTTP_USER_AGENT"]))
        if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod') || strstr($_SERVER['HTTP_USER_AGENT'],'iPad'))
            return TRUE;
              
    }
    
    function isRunningAndroidOS()
    {
//        $detect = new MobileDetect();
//        return $detect->is('AndroidOS');
        if(strstr($_SERVER['HTTP_USER_AGENT'],'Android'))
            return TRUE;
    }
    
    function isOperaMini(){
         if(preg_match('/opera mini/i',$_SERVER['HTTP_USER_AGENT']))
             return TRUE;
    }
    
    function isIEMobile(){
         if(preg_match('/IEMobile/i',$_SERVER['HTTP_USER_AGENT']))
             return TRUE;
    }        
    
    function isSymbian(){
        $detect = new MobileDetect();        
        return $detect->is('GenericPhone');        
    }

    function isRunningMobile()
    {
        $detect = new MobileDetect();
        return $detect->isMobile() || $detect->isTable();
    }
    
    function updateSitemap(){
        $curlHelper = new cURL();
        $url = Yii::app()->createAbsoluteUrl('ksitemap/index');
        $data = array();                                      
        $result = $curlHelper->post($url, $data);        
    }
    
}

?>
